﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class PRNT_DLVRY : RibbonForm
    {
        public PRNT_DLVRY()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }
        public string FileName { get; set; }

        ReportDocument rptDoc = new ReportDocument();
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public string GetLayout()
        {
            ObjectTypeManager oManager = new ObjectTypeManager();
            const int _DOCUMENT_TYPE = 3;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetPrintLayout1(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        public void ViewDeliveryLayout(string sDocNumber)
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = GetLayout();

                rptDoc.Load(sReportFile);



                this.DocumentNumber = sDocNumber;

                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_LAYOUT_DLN";

                oCommand.Parameters.Add(new SqlParameter("@DocNum", sDocNumber));
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                //System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
                //printDoc.PrinterSettings.PrinterName = "FX1175";

                //int iCount = 0;

                //for (iCount = 0; iCount <= printDoc.PrinterSettings.PaperSizes.Count - 1; iCount++)
                //{
                //    if (printDoc.PrinterSettings.PaperSizes[iCount].PaperName == "Nutratech Invoice")
                //    {
                        
                //        rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)printDoc.PrinterSettings.PaperSizes[iCount].RawKind;
                //        rptDoc.PrintOptions.PaperSource = (CrystalDecisions.Shared.PaperSource)iCount;
                //        break;
                //    }
                //}
                
                rptDoc.SetDataSource(ds.Tables[0]);

                //ReportDocument crpt = rptDoc;
                //crpt.PrintOptions.PaperSize = rptDoc.PrintOptions.PaperSize;

                //crpt.PrintToPrinter(1, false, 0, 0);

                CRViewer.ReportSource = rptDoc;
                CRViewer.Refresh();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void PRNT_INVOICE_Load(object sender, EventArgs e)
        {
            ViewDeliveryLayout(this.DocumentNumber);
        }

        private void PRNT_DLVRY_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }

        private void barPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            

                
        }

        private void barPrint_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = GetLayout();

                rptDoc.Load(sReportFile);

                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_LAYOUT_DLN";

                oCommand.Parameters.Add(new SqlParameter("@DocNum", this.DocumentNumber));
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
                printDoc.PrinterSettings.PrinterName = "FX1175";

                int iCount = 0;

                for (iCount = 0; iCount <= printDoc.PrinterSettings.PaperSizes.Count - 1; iCount++)
                {
                    if (printDoc.PrinterSettings.PaperSizes[iCount].PaperName == "Nutratech Invoice")
                    {

                        rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)printDoc.PrinterSettings.PaperSizes[iCount].RawKind;
                        rptDoc.PrintOptions.PaperSource = (CrystalDecisions.Shared.PaperSource)iCount;
                        break;
                    }
                }

                rptDoc.SetDataSource(ds.Tables[0]);

                ReportDocument crpt = rptDoc;

                crpt.PrintOptions.PaperSize = rptDoc.PrintOptions.PaperSize;
                crpt.PrintToPrinter(1, false, 0, 0);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void barRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = GetLayout();

                rptDoc.Load(sReportFile);

                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_LAYOUT_DLN";

                oCommand.Parameters.Add(new SqlParameter("@DocNum", this.DocumentNumber));
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                rptDoc.SetDataSource(ds.Tables[0]);

                CRViewer.ReportSource = rptDoc;
                CRViewer.Refresh();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void barExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barExport_ItemClick(object sender, ItemClickEventArgs e)
        {
            CRViewer.ExportReport();
        }
    }
}
